package moltencore.network.serverlobby.strategy;

import java.util.Collection;

import moltencore.network.protocol.GameListGameData;
import moltencore.network.protocol.GameListRequest;
import moltencore.network.protocol.GameListSendingEnd;
import moltencore.network.serverlobby.GameData;
import moltencore.network.serverlobby.GameList;
import spark.network.protocol.Acknowledgement;
import spark.network.protocol.Response;
import spark.network.strategy.IProtocolHandleStrategy;
import spark.network.protocol.Base;
import spark.utility.OutWrapper;

public class GameListRequestStrategy implements IProtocolHandleStrategy<GameListRequest>{

	private GameList gameList = null;

	public GameListRequestStrategy(GameList gameList){
		this.gameList = gameList;
	}

	@Override
	public void handle(GameListRequest packet, OutWrapper<Response> acknowledgement, Collection<Base> additionalData) {
		for (GameData currentGameData : gameList.listOpenGames()){
			additionalData.add(new GameListGameData(currentGameData));
		}
		additionalData.add(new GameListSendingEnd(additionalData.size()));
		acknowledgement.value = new Acknowledgement(packet);
	}
}
